import { AsyncStorage } from 'react-native';
import { ProductService } from "./services/ProductService";
import constant from "../utils/constant";
import RouterServer from "../utils/RouterServer";
const { Toast } = require('antd-mobile');
const STORAGE_KEY = "STORAGE_KEY_product";
export default {
    namespace: 'product',
    state: {
        loading: false,
        categoryList: [],
        topCategoryList: [],
        productList: [],
        product_list_next_page_url: null,
        selectedCategoryIndex: 0
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        *saveToStorage({ payload }, { call }) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload));
        },
        *restoreFromStorage({ payload }, { call, put, select }) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: Object.assign({}, JSON.parse(data))
            });
        },
        *fetchCategoryList({ payload }, { call, put, select }) {
            yield put({
                type: 'save',
                payload: { loading: true },
            });
            const auth = yield select(state => state.auth);
            const data = yield call(ProductService.getCategories, { store_id: auth.profile.id });
            yield put({
                type: 'save',
                payload: { loading: false },
            });
            if (data) {
                yield put({
                    type: 'save',
                    payload: { categoryList: data.data },
                });
                const product = yield select(state => state.product);
                if (product.categoryList.length > 0 && product.categoryList[product.selectedCategoryIndex]) {
                    yield put({
                        type: 'fetchProductList',
                        payload: { store_cate_id: product.categoryList[product.selectedCategoryIndex].id }
                    });
                }
            }
        },
        *fetchProductList({ payload }, { call, put, select }) {
            yield put({
                type: 'save',
                payload: { loading: true },
            });
            const auth = yield select(state => state.auth);
            const data = yield call(ProductService.list, Object.assign({ store_id: auth.profile.id }, payload));
            yield put({
                type: 'save',
                payload: { loading: false },
            });
            if (data) {
                const product_list_next_page_url = data.data.next_page_url ? data.data.next_page_url : null;
                yield put({
                    type: 'save',
                    payload: { productList: data.data.data, product_list_next_page_url },
                });
            }
        },
        *fetchMoreProductList({ payload }, { call, put, select }) {
            yield put({
                type: 'save',
                payload: { loading: true },
            });
            const auth = yield select(state => state.auth);
            const product = yield select(state => state.product);
            if (product.product_list_next_page_url) {
                const data = yield call(ProductService.more, product.product_list_next_page_url, Object.assign({ store_id: auth.profile.id }, payload));
                yield put({
                    type: 'save',
                    payload: { loading: false },
                });
                if (data) {
                    const product_list_next_page_url = data.data.next_page_url ? data.data.next_page_url : null;
                    let productList = Object.assign([], product.productList);
                    if (data.data.data) {
                        productList = productList.concat(data.data.data);
                    }
                    yield put({
                        type: 'save',
                        payload: { productList, product_list_next_page_url },
                    });
                }
            }
            yield put({
                type: 'save',
                payload: { loading: false },
            });
        },
        *createCategory({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            const data = yield call(ProductService.createCategory, Object.assign({ store_id: auth.profile.id }, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                Toast.success('自定义分类创建成功', constant.ToastDuration);
                yield put({
                    type: 'fetchCategoryList'
                });
                yield call(RouterServer.pop);
            }
        },
        *fetchTopCategories({ payload }, { call, put, select }) {
            const product = yield select(state => state.product);
            if (product.topCategoryList.length <= 0) {
                const data = yield call(ProductService.topCategories);
                if (data) {
                    yield put({
                        type: 'save',
                        payload: { topCategoryList: data.data }
                    });
                }
            }
        },
        *add({ payload }, { call, put, select }) {
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            const auth = yield select(state => state.auth);
            const data = yield call(ProductService.add, Object.assign({}, payload, { store_id: auth.profile.id }));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                Toast.success('商品添加成功', constant.ToastDuration);
                yield call(RouterServer.pop);
            }
        },
        *edit({ payload }, { call, put, select }) {
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            const auth = yield select(state => state.auth);
            const data = yield call(ProductService.edit, Object.assign({}, payload, { store_id: auth.profile.id }));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                Toast.success('商品修改成功', constant.ToastDuration);
            }
        },
        *del({ payload }, { call, put, select }) {
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            const auth = yield select(state => state.auth);
            const data = yield call(ProductService.del, Object.assign({}, payload, { store_id: auth.profile.id }));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                Toast.success('商品删除成功', constant.ToastDuration);
                yield call(RouterServer.pop);
                yield call(RouterServer.refresh);
            }
        },
        *changeSelectedCategoryIndex({ payload }, { put, select }) {
            let product = yield select(state => state.product);
            const selectedCategoryIndex = payload.index;
            if (product.categoryList[selectedCategoryIndex]) {
                yield put({
                    type: 'save',
                    payload: {
                        selectedCategoryIndex
                    }
                });
                yield put({
                    type: 'fetchProductList',
                    payload: {
                        store_cate_id: product.categoryList[selectedCategoryIndex].id
                    }
                });
            }
        },
        *clear({ payload }, { put }) {
            yield put({
                type: 'save',
                payload: { productList: [] }
            });
        }
    },
    subscriptions: {},
};
